// 微信订阅消息推送
import {
	subscribe,paymentMessage
} from "../common/api/index.js"

export function subscribeMessage(params) {
	// let templeteId = "1p7Z_SSvpsYroEhNz9OKedmie1bnUwzkx3XvgYD2mDg"
	let templeteId = [
	  "1p7Z_SSvpsYroEhNz9OKedmie1bnUwzkx3XvgYD2mDg"
	];
	uni.getSetting({
		withSubscriptions: true,
		success(res) {
			console.log(res, "订阅信息", res.subscriptionsSetting)
			if (!res.subscriptionsSetting.mainSwitch) {
				uni.openSetting({
					success(res) {
						console.log("打开设置页", res.authSetting)
					},
				})
			} else {
				uni.requestSubscribeMessage({
					tmplIds: ["1p7Z_SSvpsYroEhNz9OKedmie1bnUwzkx3XvgYD2mDg",
					"ECBQnn0plovzljftmioB9-wdyBncXQwcru9AF0hVxSs"],
					provider: "weixin",
					success(res) {
						console.log(
							"requestSubscribeMessage 订阅信息",
							res,
							res[templeteId]
						)

						if (res[templeteId] == "accept") {
							// 用户点击确定后
							console.log("用户订阅点击确定按钮")
							// 后端接口
							// subscribe(params).then((res) => {
							// 	console.log("订阅消息", res)
							// 	setTimeout(() => {
							// 		uni.navigateBack()
							// 	}, 2000)
							// })
							paymentMessage(params).then((res) => {
								console.log("订阅消息", res)
								setTimeout(() => {
									uni.navigateBack()
								}, 2000)
							})
							
						} else {
							console.log("拒绝，不会再弹出弹框 只能去设置页膝盖")
							uni.showModal({
								title: "您未开启消息订阅",
								content: "为了给您提供更好的服务，请您授权消息订阅",
								success: (res2) => {
									if (res2.confirm) {
										uni.openSetting({
											success(res) {
												console.log("打开设置页", res
													.authSetting)
											},
										})
									} else {
										console.log("决绝")
									}
								},
							})
						}
					},
					fail(errMessage) {
						console.log("订阅消息 失败 ", errMessage)
					},
					complete(errMessage) {
						console.log("成功 失败都执行 ", errMessage)
					},
				})
			}
		},
	})
}




// 计算差价
export function getPriceSpread() {

}

// 计算天数
export function calculateDaysBetweenTimestamps(timestamp1, timestamp2) {
	if (timestamp1 && timestamp2) {
		const startDate = new Date(timestamp1)
		const endDate = new Date(timestamp2)
		startDate.setHours(0, 0, 0, 0)
		endDate.setHours(0, 0, 0, 0)

		if (endDate.getTime() == startDate.getTime()) {
			return 1
		}
		if (startDate < endDate) {
			const timeDifference = endDate.getTime() - startDate.getTime()
			const daysDifference = Math.floor(
				timeDifference / (1000 * 60 * 60 * 24)
			)
			// 四舍五入到最近的整数
			// daysDifference = Math.round(daysDifference);

			return daysDifference + 1
		}
		if (startDate > endDate) {
			// uni.showToast({
			// 	title: "时间范围不正确！",
			// 	icon: "none",
			// })
			return 0
		}
	} else {
		return 0
	}
}